﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using TouchUI;

namespace MobileStore.WinPhone
{
    public class MainDialogListItem : ScrollListItem
    {
        private Car car;
        private Bitmap image;
        private bool loadingImage;

        public MainDialogListItem(Car car, int screenFactor)
        {
            this.car = car;
            this.ScreenFactor = screenFactor;
        }

        public override void Paint(Graphics g)
        {
            if(this.image == null)
            {
                this.image = Common.Values.LoadBitmap("nos.jpg", 70, 52);
                if(car.Code != "no")
                {
                    Thread loadImageThread = new Thread(new ThreadStart(loadImage));
                    loadImageThread.Start();
                    loadingImage = true;
                }
            }

            Brush fontBrush;
            Pen pen;
            if(!Selected)
            {
                fontBrush = Common.Values.FontBrush;
                pen = Common.Values.Pen2;
            }
            else
            {
                fontBrush = Common.Values.HighTextBrush;
                pen = Common.Values.HighTextPen2;
            }
            lock(this.image)
                g.DrawImage(image, Rectangle.Left + ScreenFactor, Rectangle.Top + ScreenFactor);
            if(loadingImage)
                g.DrawString(Strings.Loading, Common.Values.SmallFont, Common.Values.FontBrush, Rectangle.Left + 4 * ScreenFactor, Rectangle.Top + 2 * ScreenFactor);

            string s = Common.Values.TruncateOneLine(g, car.Model, Common.Values.Font, Rectangle.Width - 5 - 70 - 16 * ScreenFactor);
            Size size = g.MeasureString(s, Common.Values.Font).ToSize();
            g.DrawString(s, Common.Values.Font, fontBrush, Rectangle.Left + (5 + 70) * ScreenFactor, Rectangle.Top + 8 * ScreenFactor);
            s = Common.Values.TruncateOneLine(g, "$" + car.Price.ToString() + ", " + car.Year + ", " + (car.Km / 20).ToString() + " miles",
                Common.Values.SubFont, Rectangle.Width - 5 - 70 - 16 * ScreenFactor);
            g.DrawString(s, Common.Values.SubFont, fontBrush, Rectangle.Left + (5 + 70) * ScreenFactor,
                Rectangle.Top + size.Height + (8 + 4) * ScreenFactor);

            int x1 = Rectangle.Left + Rectangle.Width - 12 * ScreenFactor;
            int x2 = x1 + 4 * ScreenFactor;
            int y2 = Rectangle.Top + Rectangle.Height / 2;
            int y1 = y2 - 4 * ScreenFactor;
            int y3 = y2 + 4 * ScreenFactor;
            g.DrawLines(pen, new Point[] { new Point(x1, y1), new Point(x2, y2), new Point(x1, y3) });
        }

        private void loadImage()
        {
            Bitmap image = Common.Values.LoadBitmap(new Uri(Common.Values.ImageUrl + car.Code + "s.jpg"), 70, 52);
            lock(this.image)
                this.image = image;
            loadingImage = false;
        }
    }
}
